#
#    This file is part of the substandard lisp implementation of Standard Lisp
#    Copyright (C) 2011 Nicky Nickell
#
#    substandard lisp is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys

infile = open(sys.argv[1])

table = {}

equal = '''  lisp.evaluate('%s');
  ok((lisp.exception == false) && (lisp.result == '%s'), '%s');'''
ok = '''  lisp.evaluate('%s');
  ok((lisp.exception == false) && /%s/.test(lisp.result), '%s');'''
error = '''  lisp.evaluate('%s');
  ok((lisp.exception == true) && /%s/.test(lisp.result), '%s');'''
evall = "  lisp.evaluate('%s');"

module = ''
test = ''

for line in infile:
    line = line.strip().replace("'", "\\'")
    if line == '':
        continue
    elif line.startswith('module'):
        module = line.split(' ', 1)[1]
        table[module] = {}
    elif line.startswith('test'):
        test = line.split(' ', 1)[1]
        table[module][test] = []
    elif ' -> ' in line:
        l = line.split(' -> ')
        table[module][test].append(equal % (l[0], l[1], line))
    elif ' -r> ' in line:
        l = line.split(' -r> ')
        table[module][test].append(error % (l[0], l[1], line))
    elif ' -m> ' in line:
        l = line.split(' -m> ')
        table[module][test].append(ok % (l[0], l[1], line))
    else:
        table[module][test].append(evall % line)

for mod in table:
    print "module('%s');\n" % mod
    for tes in table[mod]:
        print "test('%s', function() {" % tes
        for sta in table[mod][tes]:
            print sta
        print "});\n"
